#include "pmm.h"
#include "riscv.h"
#include "process.h"
#include "util/functions.h"
#include "spike_interface/spike_file.h"
#include "spike_interface/spike_utils.h"

int do_open(char *pathname, int flags) {
    spike_file_t *f = spike_file_open(pathname, flags, 0);
    if ( (int64)f < 0 )
        return -1;
    int fd = spike_file_dup(f);
    if (fd < 0) {
        spike_file_decref(f);
        return -1;
    }else
        return fd;
}

int do_read(int fd, char *buf, uint64 count) {
    spike_file_t *f = spike_file_get(fd);
    return spike_file_read(f, buf, count);
}

int do_write(int fd, char *buf, uint64 count) {
    spike_file_t *f = spike_file_get(fd);
    return spike_file_write(f, buf, count);
}

int do_close(int fd) {
    spike_file_t *f = spike_file_get(fd);
    return spike_file_close(f);
}